<template>
  <div>
    <div slot="com_header"><bread></bread></div>
    <div slot="com_con">
      <!-- 修改弹窗 -->

      <el-dialog title="收货地址" :visible.sync="upddialog">
        <el-form :model="goodsForm">
          <el-form-item label="商品名称" label-width="120px">
            <el-input
              v-model="goodsForm.goods_name"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="商品价格" label-width="120px">
            <el-input
              v-model="goodsForm.goods_price"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="商品数量" label-width="120px">
            <el-input
              v-model="goodsForm.goods_number"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="商品重量" label-width="120px">
            <el-input
              v-model="goodsForm.goods_weight"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="商品介绍" label-width="120px">
            <el-input
              v-model="goodsForm.goods_introduce"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="upddialog = false">取 消</el-button>
          <el-button type="primary" @click="upd()">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 条件查询 -->
      <el-form :inline="false" :model="form" class="demo-form-inline">
        <el-row>
          <el-col :md="16">
            <el-form-item>
              <el-input v-model="form.query" placeholder="用户名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :md="2">
            <el-form-item>
              <el-button type="normal" @click="query">查询</el-button>
            </el-form-item>
          </el-col>
          <el-col :md="2">
            <el-form-item>
              <el-button type="primary" @click="goAdd">添加</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 列表查询 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="goods_id" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="180">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
        <el-table-column prop="goods_number" label="商品数量"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column
          prop="goods_introduce"
          label="商品介绍"
        ></el-table-column>

        <el-table-column label="创建时间">
          <template slot-scope="{ row }">
            {{ row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button type="warning" size="mini" @click="open(row)">
              编辑
            </el-button>
            <el-button type="danger" size="mini" @click="del(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="form.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import bread from "../../components/bread";
export default {
  components: {
    bread,
  },
  data() {
    return {
      upddialog: false,
      tableData: [],
      form: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      // 修改商品的form
      goodsForm: {
        goods_id: "",
        goods_name: "",
        goods_price: "",
        goods_number: "",
        goods_weight: "",
        goods_introduce: "",
        pics: [],
        attrs: [],
      },
    };
  },
  mounted() {
    this.query();
  },
  methods: {
    //获取角色列表
    async query() {
      let rst = await this.$http.get("goods", this.form);
      let { data, meta } = rst;
      data.goods.map((item) => {
        item.flag = true;
      });
      console.log(data);
      this.tableData = data.goods;
      this.total = data.total;
      // this.tableData = rst.data;
    },

    // 分页
    handleSizeChange(val) {
      this.form.pagesize = val;
      this.query();
    },
    handleCurrentChange(val) {
      this.form.pagenum = val;
      this.query();
    },

    //删除商品
    async del(row) {
      let rst = await this.$http.delete(`goods/${row.goods_id}`);
      if (rst.meta.status == 200) {
        this.$message.success(rst.meta.msg);
        this.query();
      }
    },

    // 跳转添加页面
    goAdd() {
      this.$router.replace("/goods/add");
    },

    //修改商品
    async open(row) {
      console.log(row);
      let rst = await this.$http.get(`goods/${row.goods_id}`);
      this.goodsForm = rst.data;
      console.log(rst);
      this.upddialog = true;
    },
    async upd() {
      let {
        goods_name,
        goods_price,
        goods_number,
        goods_weight,
        goods_introduce,
        attrs,
        pics
      } = this.goodsForm;
      let rst = await this.$http.put(`goods/${this.goodsForm.goods_id}`, {
        goods_name,
        goods_price,
        goods_number,
        goods_weight,
        goods_introduce,
        attrs,
        pics
      });
      // this.query()
      console.log(rst);
      this.upddialog = false;
    },
  },
};
</script>

<style lang="less" scoped>
</style>